﻿/*
 退回属性模块
 设置流程可退回到的节点
 */

var ReturnProperty = {

    ActivityProperty: {
        CanReturnRule: {
            'title': "可退回节点",
            'control': $("<select id='selectCanReturnRule' multiple='multiple' class='select'>")
        },
        ReturnRule: {
            'title': "退回节点",
            'control': $("<select id='selectReturnRule' multiple='multiple' class='select'>")
        }

    },

    InitialPage: function (parent, activity) {
        var me = this;
        me.activity = activity;
        me.InitialControl(parent);
        me.InitialData(activity);
    },

    //初始化控件
    InitialControl: function (parent) {
        var me = this;

        var divCanReturn = me.InitialSelect(me.ActivityProperty.CanReturnRule);

        // 按钮区
        var divButton = $("<div style='width:15px; float:left; margin-top:100px; font-size:12px;'>");


        var btnRight = $("<input type='button' value='右移'>");
        divButton.append(btnRight);
        var btnLeft = $("<input type='button' value='左移' style=' margin-top:5px; '>");
        divButton.append(btnLeft);

        btnRight.click(function () {

            var query = $('#selectCanReturnRule option:selected')
            $.each(query, function (i, val) {
                $(val).remove();
                me.ActivityProperty.ReturnRule.control.append($(val));
            })
        });
        btnLeft.click(function () {
            var query = $("#selectReturnRule  option:selected");
            $.each(query, function (i, val) {
                $(val).remove();
                me.ActivityProperty.CanReturnRule.control.append($(val));
            })
        });

        //以选区
        var divReturn = me.InitialSelect(me.ActivityProperty.ReturnRule);

        parent.append(divCanReturn);
        parent.append(divButton);
        parent.append(divReturn);
    },

    //初始化数据（给控件绑定数据）
    InitialData: function (activity) {
        var me = this;
        $.each(activityHelper.Activitys, function (i, val) {
            val.LoopCount = 0;
        })
        activityHelper.GetReturnActivitys(activityHelper.DraftingActivity);
        // me.activity.GetReturnActivitys(true, me.activity.ID);
        if (activity.ActivityProperty != undefined && activity.ActivityProperty.WF_RULE != undefined) {
            //待选择项
            var tmp = new Array();
            for (i = 0; i < me.activity.Preactivity.length; i++) {
                if (me.activity.Preactivity[i].activityType != ActivityType.Condition && me.activity.Preactivity[i].activityType != ActivityType.Merging && me.activity.Preactivity[i].activityType != ActivityType.Branching)
                    if (activity.ActivityProperty.WF_RULE.where(me.activity.Preactivity[i].guid, 'toGuid') == undefined)
                        tmp[tmp.length] = me.activity.Preactivity[i];
            }
            $.each(tmp, function (i, val) {
                if (val.guid != me.activity.guid) {
                    var option = $("<option value=" + val.guid + " title=" + val.ActivityName + ">" + val.ActivityName + "</option>");
                    me.ActivityProperty.CanReturnRule.control.append(option);
                }
            });

            tmp = new Array();
            for (j = 0; j < activity.ActivityProperty.WF_RULE.length; j++) {
                var preac = me.activity.Preactivity.where(activity.ActivityProperty.WF_RULE[j].toGuid, 'guid');
                if (preac && !tmp.where(preac.guid))
                    tmp[tmp.length] = preac;
            }
            $.each(tmp, function (i, val) {
                var option = $("<option value=" + val.guid + " title=" + val.ActivityName + ">" + val.ActivityName + "</option>");
                me.ActivityProperty.ReturnRule.control.append(option);
            });
        } else {
            $.each(me.activity.Preactivity, function (i, val) {
                if (val.activityType != ActivityType.Condition && val.activityType != ActivityType.Merging && val.activityType != ActivityType.Branching) {
                    var option = $("<option value=" + val.guid + ">" + val.ActivityName + "</option>");
                    me.ActivityProperty.CanReturnRule.control.append(option);
                }
            });
        }
    },

    //保存数据
    SaveData: function (activity) {
        if (activity.ActivityProperty == undefined)
            activity.ActivityProperty = {};
        activity.ActivityProperty.WF_RULE = new Array();
        var query = $("#selectReturnRule  option");
        var j = 0;
        $.each(query, function (i, val) {
            var rule = new Object();
            rule.guid = Guid.NewGuid();

            rule.templateId = '';
            rule.ruleName = '';
            rule.IS_DEFAULT = 0;
            rule.description = '';
            rule.condition = '';
            rule.coordinate = '';
            rule.fromGuid = activity.guid;
            rule.toGuid = $(val).val();
            rule.ruleType = 0;
            rule.ruleType = 1;
            rule.isDel = 0;
            rule.CREATOR = GetCookie('TokenGuid');

            activity.ActivityProperty.WF_RULE[j] = rule;
            ++j;

        });

        activity.ActivityProperty.ENABLE_RETURN = activity.ActivityProperty.WF_RULE > 0 ? 0 : 1;
    },

    //选择区域
    InitialSelect: function (ReturnProp) {
        var me = this;
        ReturnProp.control.empty()
        var divCanReturn = $("<div style=' width:250px; float:left; margin-left:25px;margin-right:25px;'>");
        divCanReturn.append($("<div style='width:250px; float:left; height:30px; line-height:30px; text-align:center;font-size:14px;font-weight:bold;'><h3>" + ReturnProp.title + "</h3></div>"));

        var divCanReturnList = $("<div style=' width:250px; text-align:center;'>");
        divCanReturn.append(divCanReturnList);
        divCanReturn.append(ReturnProp.control);
        return divCanReturn;
    }


}

